GAMES101LAB7
这次实验卡了很久啊,难QAQ,最后许多代码都是在别人博客上抄的(难绷
任务一:迁移代码并完成CastRay
首先需要迁移代码,将上节课完成的代码迁移过来
然后完成castray
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
Vector3f Scene::castRay(const Ray &ray, intdepth) const
{
auto hitObj =intersect(ray);
if (!hitObj.happened) return {};
returnshade(hitObj,-ray.direction);
}
|
然后完成多线程加速,也就是将渲染分块进行,修改render下。
为了方便得到不同的spp结果可以自行传入spp和文件名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| void Renderer::Render(const Scene&scene,intspp, std::string&file_name)
{
std::vector<Vector3f>framebuffer(scene.width*scene.height);
float scale =tan(deg2rad(scene.fov*0.5));
float imageAspectRatio =scene.width/ (float)scene.height;
Vector3f eye_pos(278, 273, -800);
int m =0;
g_complateTotals =0;
std::cout <<"SPP: "<< spp <<"\n";
int numThreads = std::thread::hardware_concurrency();
int lines =scene.height/ numThreads +1;
std::vector<std::thread> wokers;
for (int i =0; i < numThreads; i++)
{
int y0 = i * lines;
int y1 = std::min(y0 + lines,scene.height);
std::cout <<"id:"<<i <<""<< y0 <<"=>"<< y1 << std::endl;
wokers.push_back(std::thread(render_thread,std::ref(framebuffer),std::ref(scene),spp,y0,y1));
}
for (int i =0;i<wokers.size();i++)
{
wokers[i].join();
}
UpdateProgress(1.f);
FILE* fp =fopen(file_name.c_str(), "wb");
(void)fprintf(fp, "P6\n%d%d\n255\n", scene.width, scene.height);
for (auto i =0; i <scene.height*scene.width; ++i) {
staticunsignedcharcolor[3];
color[0] = (unsignedchar)(255* std::pow(clamp(0, 1, framebuffer[i].x), 0.6f));
color[1] = (unsignedchar)(255* std::pow(clamp(0, 1, framebuffer[i].y), 0.6f));
color[2] = (unsignedchar)(255* std::pow(clamp(0, 1, framebuffer[i].z), 0.6f));
fwrite(color, 1, 3, fp);
}
fclose(fp);
}
|
不同spp结果
这里只取了16、512、2048展示
spp=16
spp=1512
spp=2048
时间和spp成正比,其中16版本花费207s
任务二:完成MICROFACE
这里几乎就是纯copy了,详情见GAMES101作业7及课程总结(重点实现多线程加速,微表面模型材质)
不过不知道为什么我的lambda总是报错,于是把它变成成员函数了
结果对比一下(spp都等于16)
diffuse
MICROFACE